﻿using ApiDoc.DAL;
using ApiDoc.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ApiDoc.Admin.Server.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class FolderController : ControllerBase
    {
        private readonly FolderDAL folderDAL;
        public FolderController(FolderDAL folderDAL)
        {
            this.folderDAL = folderDAL;
        }

        [HttpPost("SaveFolder")]
        public FolderModel SaveFolder(FolderModel info)
        {
            if (info.SN > 0)
            {
                this.folderDAL.Update(info);
            }
            else
            { 
                info.SN = this.folderDAL.Insert(info);
            }
            return info;
        }

      
        [HttpGet("QueryAll")]
        public List<FolderModel> QueryAll()
        {
            List<FolderModel> list = this.folderDAL.QueryAll();
            return list;
        }

        [HttpGet("Query")]
        public List<FolderModel> Query(int parentSN)
        {
            List<FolderModel> list = this.folderDAL.QueryChild(parentSN);
            return list;
        }

        [HttpGet("Delete")]
        public int DeleteFolder(int SN)
        {
            List<FolderModel> list = this.folderDAL.QuerySelfAndChilds(SN);

            int count = 0;
            foreach (FolderModel folderModel in list)
            {
                int result = this.folderDAL.Delete(folderModel);
                count++;
            }

            return count;
        }
    }
}
